home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1994 November / Cd Ware (Nro. 2) - Epimundo.iso / DOS / PG / OOLIFE.ZIP / LIFE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-09  |  3.1 KB  |  159 lines

  1. #include <math.h>
  2. #include <graphics.h>
  3. #include "ca.h"
  4. #define NULL 0
  5. void CLife::cycle()
  6. {
  7.     if(!dirty)        //If none of the neighbors changed
  8.         return;    
  9.  
  10.     short live;
  11.     live = neighbors.countLive();
  12.  
  13.     if(live==2 || live == 3)
  14.         {
  15.         if(!lifeState && live == 3)
  16.             {
  17.             newState = TRUE;    //A better implementation would have a newstate
  18.             neighbors.dirty();    //that updates the dirty neighbors
  19.             graphic->draw(TRUE);
  20.             }
  21.         }
  22.     else
  23.         {
  24.         if(lifeState)
  25.             {
  26.             newState = FALSE;
  27.             neighbors.dirty();
  28.             graphic->draw(FALSE);
  29.             }
  30.         }
  31. }
  32.  
  33. short CLifeList::countLive()
  34. {
  35.     CNode *counter;
  36.     short liveCount =0;
  37.     counter = top;
  38.  
  39.  
  40.     while(counter)    //Loop until null pointer
  41.         {
  42.         if((counter->getLife())->isLive())
  43.             liveCount ++;
  44.         counter = counter->getNext();
  45.         }
  46.  
  47.     return liveCount;
  48. }
  49.  
  50. short CLifeList::addLife(CLife *toAdd)
  51. {
  52.     CNode *newNode = new CNode;
  53.     if(!cells)                           //check for initial cell
  54.         {
  55.         top = newNode;
  56.         bottom = newNode;
  57.         }
  58.     else
  59.         {
  60.         newNode->setPrev(bottom);
  61.         bottom->setNext(newNode);
  62.         bottom = newNode;
  63.         }
  64.     newNode->setLife(toAdd);
  65.     current = newNode;        //make the new one current (like we care!!!)
  66.     cells++;                //Cells counter
  67.     return 0;
  68. }
  69.  
  70. void CLifeList::dirty()
  71. {
  72.     CNode *counter;
  73.     counter = top;
  74.  
  75.     while(counter)    //Loop until null pointer
  76.         {
  77.         (counter->getLife())->setDirty();
  78.         counter = counter->getNext();
  79.         }
  80. }
  81.  
  82. void CLifeList::associateCells()
  83. {
  84.     long counter;
  85.     //We are going to make this into a circular list for the length of
  86.     //this routine.
  87.     //In the words of Dewhurst/Stark "The only positive point one can make
  88.     //about the implementation is that it took only a few minutes to write"
  89.     top->setPrev(bottom);
  90.     bottom->setNext(top);
  91.  
  92.     CNode *back, *front, *temp;
  93.     long  root;
  94.     back=top;
  95.     front=top;
  96.     root = sqrt(cells);
  97.     for(counter=0;counter<root;counter++)    //Get pointed to the neighbors
  98.         {
  99.         back=back->getPrev();
  100.         front=front->getNext();
  101.         }
  102.  
  103.     temp=top;
  104.     for(counter=0;counter<cells;counter++)    //register the neighbors
  105.         {
  106.         (temp->getLife())->addNeighbor((back->getPrev())->getLife());
  107.         (temp->getLife())->addNeighbor(back->getLife());
  108.         (temp->getLife())->addNeighbor((back->getNext())->getLife());
  109.         (temp->getLife())->addNeighbor((front->getPrev())->getLife());
  110.         (temp->getLife())->addNeighbor(front->getLife());
  111.         (temp->getLife())->addNeighbor((front->getNext())->getLife());
  112.         (temp->getLife())->addNeighbor((temp->getPrev())->getLife());
  113.         (temp->getLife())->addNeighbor((temp->getNext())->getLife());
  114.         back = back->getNext();
  115.         front = front->getNext();
  116.         temp = temp->getNext();
  117.         }
  118.  
  119.     //Make the list non-circular again
  120.     top->setPrev(NULL);
  121.     bottom->setNext(NULL);
  122. }
  123.  
  124. void CLifeList::cycle()
  125. {
  126.     CNode *temp;
  127.  
  128.     temp = top;
  129.     while(temp)
  130.         {
  131.         (temp->getLife())->cycle();
  132.         temp = temp->getNext();
  133.         }
  134. }
  135. void CLifeList::update()
  136. {
  137.     CNode *temp;
  138.  
  139.     temp = top;
  140.     while(temp)
  141.         {
  142.         (temp->getLife())->update();
  143.         temp = temp->getNext();
  144.         }
  145. }
  146.  
  147. void CGraphic::draw(BOOL on)
  148. {
  149.     if(on)
  150.         setcolor(15);
  151.     else
  152.         setcolor(0);
  153.  
  154.     rectangle ( x * 5, y * 5, x * 5 + 4, y * 5 + 4 );
  155.     return;
  156. }
  157.  
  158.  
  159.